package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.jopo.Classes;
import com.itheima.jopo.PageBean;
import com.itheima.mapper.ClassesMapper;
import com.itheima.service.ClassesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * ClassesServiceImpl
 *
 * @author liliudong
 * @version 1.0
 * @description ClassesService
 * @date 2023/5/1 10:53
 */
@Service
@RequiredArgsConstructor
public class ClassesServiceImpl implements ClassesService {

    private final ClassesMapper classesMapper;

    @Override
    public void save(Classes classes) {
        classes.setCreateTime(LocalDateTime.now());
        classes.setUpdateTime(LocalDateTime.now());
        classesMapper.insert(classes);
    }

    @Override
    public void removeById(Integer id) {
        classesMapper.deleteById(id);
    }

    @Override
    public void updateById(Classes classes) {
        if (Objects.isNull(classes.getId())) {
            return;
        }
        classes.setUpdateTime(LocalDateTime.now());
        classesMapper.updateById(classes);
    }

    @Override
    public Classes getById(Integer id) {
        return classesMapper.selectById(id);
    }

    @Override
    public PageBean selectPage(Integer page, Integer pageSize, String name, LocalDate begin, LocalDate end) {
        PageHelper.startPage(page, pageSize);
        List<Classes> classesList = classesMapper.selectList(name, begin, end);
        PageInfo<Classes> classesPageInfo = new PageInfo<>(classesList);
        return new PageBean(classesPageInfo.getTotal(), classesPageInfo.getList());
    }

    @Override
    public boolean existByName(String name) {
        Classes classes = new Classes();
        classes.setName(name);
        return classesMapper.selectCount(classes) > 0;
    }
}
